package com.controller.dataSource;

import com.model.pojo.annotation.CheckToken;
import com.model.bo.BaseToDataInfo;
import com.model.bo.GroupUpdateId;
import com.model.bo.ToSql;
import com.model.pojo.RepEntity;
import com.model.pojo.TestPage;
import com.model.vo.configVo.ChangeOrderInfo;
import com.model.vo.configVo.DataConnectorInfo;
import com.model.vo.configVo.GroupInfo;
import com.server.dataSource.DataConnectorService;
import com.server.dataSource.ImplementSqlService;
import com.model.pojo.annotation.TargetDataSource;
import com.config.RoleInterceptor.Auth;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/Connector")
public class DataConnectorController {
    @Autowired
    DataConnectorService dataConnectorService;

    @Autowired
    ImplementSqlService implementSqlService;

    /*
    保存数据源配置
     */
    @CheckToken
    @Auth(user = "admin")
    @RequestMapping("/inputDataConnector")
    public RepEntity inputDataConnector(@RequestHeader String token, @RequestBody DataConnectorInfo body) {
        System.out.println("-----------");
        System.out.println(body);
        return dataConnectorService.setDataConnector(token,body);
    }

    /*
    查看数据源列表
     */
    @CheckToken
    @RequestMapping("/getListDataConnector")
    public RepEntity getListDataConnector(@RequestHeader String token, @RequestBody TestPage body){
        return dataConnectorService.getListConnector(token, body);
    }

    /*
    查看单个数据源配置
     */
    @CheckToken
    @Auth(user = "admin")
    @RequestMapping("/getDataConnector")
    public RepEntity getDataConnector(@RequestHeader String token,@RequestBody int body){
        return dataConnectorService.getConnector(body);
    }

    /*
    更新数据源配置
     */
    @CheckToken
    @Auth(user = "admin")
    @RequestMapping("/updateData")
    public RepEntity updateData(@RequestHeader String token,@RequestBody DataConnectorInfo body){
        return dataConnectorService.updateData(body);
    }

    /*
    转交数据源
     */
    @CheckToken
    @RequestMapping("/changeDbOrder")
    public RepEntity changeDbOrder(@RequestHeader String token,@RequestBody ChangeOrderInfo body){
        return dataConnectorService.changeOrder(body, token);
    }

    /*
    更新数据源配置的分组
     */
    @Auth(user = "admin")
    @CheckToken
    @RequestMapping("/updateConnectConfigGroup")
    public RepEntity updateConConfigGroup(@RequestHeader String token,@RequestBody GroupUpdateId body){
        return dataConnectorService.updateConfigGroup(body);
    }


    /*
    删除配置
     */
    @Auth(user = "admin")
    @CheckToken
    @RequestMapping("/delDataConnector")
    public RepEntity delDataConnector(@RequestHeader String token,@RequestBody List<Integer> body){
        return dataConnectorService.delDataConnector(body);
    }

    /*
    执行数据源配置
    */
    @TargetDataSource
    @CheckToken
    @RequestMapping("/implementSql")
    @Auth(user = "admin")
    public RepEntity implementSql(@RequestHeader String token,@RequestBody ToSql body){
        return implementSqlService.implementSql(body);
    }

    /*
    添加数据源分组
     */
    @CheckToken
    @RequestMapping("/setConnectorGroup")
    @Auth(user = "admin")
    public RepEntity setConnectorGroup(@RequestHeader String token,@RequestBody GroupInfo body){
        return dataConnectorService.setDataConnectorGroup(body);
    }

    /*
    更新数据源分组
     */
    @CheckToken
    @Auth(user = "admin")
    @RequestMapping("/updataDataConnectorGroup")
    public RepEntity updataDataConnectorGroup(@RequestHeader String token,@RequestBody GroupInfo body){
        return dataConnectorService.updataDataConnectorGroup(body);
    }

    /*
    批量更新数据源分组
     */
    @CheckToken
    @Auth(user = "admin")
    @RequestMapping("/updataConnectorGroup")
    public RepEntity updataConnectorGroup(@RequestHeader String token,@RequestBody List<GroupInfo> body){
        return dataConnectorService.updataConnectorGroups(body);
    }

    /*
    删除数据源分组
     */
    @Auth(user = "admin")
    @CheckToken
    @RequestMapping("/delDataConnectorGroup")
    public RepEntity delDataConnectorGroup(@RequestHeader String token,@RequestBody List<Integer> body){
        return dataConnectorService.delDataConnectorGroup(body);
    }

    /*
    查询数据源分组
     */
    @CheckToken
    @RequestMapping("/getConnectorGroup")
    public RepEntity getConnectorGroup(@RequestHeader String token){
        return dataConnectorService.getConnectorGroup();
    }

    /*
    查询数据源数据
     */
    @CheckToken
    @RequestMapping("/getConnectorData")
    public RepEntity getConnectorData(@RequestHeader String token, @RequestBody BaseToDataInfo body){
        return dataConnectorService.getConnectorData(token, body);
    }
}
